/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import entidades.Tanque;
import entidades.TipoCombustible;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import rn.CombustibleRN;
import rn.VehiculoRN;
import utils.JTextFieldToUpperCase;

/**
 *
 * @author Pablo
 */
public class NuevoTanque extends javax.swing.JFrame {

    /**
     * Creates new form NuevoTanque
     */
    Tanque t;

    public NuevoTanque(java.awt.Frame parent, String titulo, Tanque t) {
        //super(parent, titulo);
        initComponents();
        this.setLocationRelativeTo(null);
        
        tf_descripcion.setDocument(new JTextFieldToUpperCase());
        tf_ubicacion.setDocument(new JTextFieldToUpperCase());                

        this.t = t;
        List<TipoCombustible> todosTipoCombustible = CombustibleRN.getTodosTipoCombustible();
        for (TipoCombustible tipo : todosTipoCombustible) {
            combo_tipocombustible.addItem(tipo);
        }

        if (t != null) {

            tf_descripcion.setText(t.getDescripcion());
            combo_tipocombustible.setSelectedItem(t.getTipoCombustible());
            tf_ubicacion.setText(t.getUbicacion());
            tf_capacidad.setText(t.getCapacidad_litros());
            tf_disponibilidad.setText(""+t.getDisponible_litros());

        }
        
        bt_aceptar.addKeyListener(new GrabarRegistro());
        tf_disponibilidad.addKeyListener(new KeyAdapter() {            
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    bt_aceptar.requestFocus();
                }
            }
        });
    }
    
    class GrabarRegistro extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getSource() == bt_aceptar) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    grabar();
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tf_descripcion = new javax.swing.JTextField();
        bt_aceptar = new javax.swing.JButton();
        bt_cancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        tf_disponibilidad = new javax.swing.JTextField();
        combo_tipocombustible = new javax.swing.JComboBox();
        bt_add_tipocombustible = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tf_ubicacion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tf_capacidad = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("  Descripción:");

        tf_descripcion.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        bt_aceptar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_aceptar.setText("Aceptar");
        bt_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_aceptarActionPerformed(evt);
            }
        });

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("Cancelar");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("  Disponibilidad de Litos:");

        tf_disponibilidad.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_disponibilidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_disponibilidadActionPerformed(evt);
            }
        });

        combo_tipocombustible.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_tipocombustible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_tipocombustibleActionPerformed(evt);
            }
        });

        bt_add_tipocombustible.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        bt_add_tipocombustible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_add_tipocombustibleActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("  Tipo de Combustible:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("  Ubicación:");

        tf_ubicacion.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("  Capacidad de Litros:");

        tf_capacidad.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("(Ltrs.)");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("(Ltrs.)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(tf_disponibilidad, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addGap(0, 25, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(40, 40, 40))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(23, 23, 23)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_ubicacion, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(combo_tipocombustible, javax.swing.GroupLayout.Alignment.TRAILING, 0, 219, Short.MAX_VALUE)
                            .addComponent(tf_descripcion, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tf_capacidad))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(bt_add_tipocombustible, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bt_aceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_cancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_tipocombustible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addComponent(bt_add_tipocombustible, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_ubicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_capacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_disponibilidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_aceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_aceptarActionPerformed
        grabar();
    }//GEN-LAST:event_bt_aceptarActionPerformed

    private void grabar(){
        Tanque p = new Tanque();
        if (verificarCampo(tf_descripcion)) {
            if (this.t == null) {
                p.setDescripcion(tf_descripcion.getText());
                p.setTipoCombustible((TipoCombustible) combo_tipocombustible.getSelectedItem());
                p.setUbicacion(tf_ubicacion.getText());
                p.setCapacidad_litros(tf_capacidad.getText());
                p.setDisponible_litros(Double.parseDouble(tf_disponibilidad.getText()));

                if (!CombustibleRN.yaExisteTanque(tf_descripcion.getText(), null)) {
                    CombustibleRN.create(p);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Tanque ya existe!!!");
                }

            } else {
                t.setDescripcion(tf_descripcion.getText());
                t.setTipoCombustible((TipoCombustible) combo_tipocombustible.getSelectedItem());
                t.setUbicacion(tf_ubicacion.getText());
                t.setCapacidad_litros(tf_capacidad.getText());
                p.setDisponible_litros(Double.parseDouble(tf_disponibilidad.getText()));

                if (!CombustibleRN.yaExisteTanque(tf_descripcion.getText(), t.getId())) {
                    CombustibleRN.edit(t);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "Tanque ya existe!!!");
                }
            }
            ABM_Tanques.actualizarTabla();
        } else {
            JOptionPane.showMessageDialog(null, "Descripcion no puede ser vacia");
        }

    }
    
    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelarActionPerformed

    private void bt_add_tipocombustibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_add_tipocombustibleActionPerformed
        new NuevoTipoCombustible(this, "Nuevo Tipo de Combustible", null, false);
    }//GEN-LAST:event_bt_add_tipocombustibleActionPerformed

    private void tf_disponibilidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_disponibilidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_disponibilidadActionPerformed

    private void combo_tipocombustibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_tipocombustibleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_combo_tipocombustibleActionPerformed

    private boolean verificarCampo(JTextField f) {
        if (f.getText().isEmpty() || f.getText().startsWith(" ")) {
            return false;
        }
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_aceptar;
    private javax.swing.JButton bt_add_tipocombustible;
    private javax.swing.JButton bt_cancelar;
    public static javax.swing.JComboBox combo_tipocombustible;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField tf_capacidad;
    private javax.swing.JTextField tf_descripcion;
    private javax.swing.JTextField tf_disponibilidad;
    private javax.swing.JTextField tf_ubicacion;
    // End of variables declaration//GEN-END:variables
}
